/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.testis.client;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * A class to initialize beans created in configuration files
 * 
 * @author nurhat
 */
public class TestisApplicationContext {

    private static AbstractApplicationContext m_loggingContext = null;
    private static AbstractApplicationContext m_loggingContextForClient = null;
    private static AbstractApplicationContext m_generalContext = null;
    private static AbstractApplicationContext m_runnerContext = null;

    /**
     * A function to initialize beans stored in all config files
     */
    public static void initialize() {

	getGeneralContext();
	getLoggingContext();
    }

    /**
     * A function to initialize beans stored in all config files
     */
    public static void initializeForClient() {

	getGeneralContext();
	getLoggingContextForClient();

    }

    /**
     * A function to initialize beans stored in all config files
     */
    public static void initializeForRunner() {

	getRunnerContext();
	getLoggingContextForClient();

    }

    /**
     * A function to initialize beans stored in moduleConnectorDefinitions.xml
     * file
     */
    public static AbstractApplicationContext getGeneralContext() {
	if (m_generalContext == null) {
	    m_generalContext = new ClassPathXmlApplicationContext(
		    "/com/airties/atof/configuration/moduleConnectorDefinitions.xml");
	    m_generalContext.registerShutdownHook();

	}
	return m_generalContext;
    }

    /**
     * A function to initialize beans stored in
     * loggingModuleQueueDefinitions.xml file
     */
    public static AbstractApplicationContext getLoggingContext() {
	if (m_loggingContext == null) {
	    m_loggingContext = new ClassPathXmlApplicationContext(
		    "/com/airties/atof/configuration/loggingProviderDefinitions.xml");
	    m_loggingContext.registerShutdownHook();
	}

	return m_loggingContext;
    }

    public static AbstractApplicationContext getLoggingContextForClient() {
	if (m_loggingContextForClient == null) {
	    m_loggingContextForClient = new ClassPathXmlApplicationContext(
		    "/com/airties/atof/configuration/loggingProviderDefinitionsForClient.xml");
	    m_loggingContextForClient.registerShutdownHook();

	}
	return m_loggingContextForClient;
    }

    /**
     * @return the m_runnerContext
     */
    public static AbstractApplicationContext getRunnerContext() {
	if (m_runnerContext == null) {
	    m_runnerContext = new ClassPathXmlApplicationContext(
		    "/com/airties/atof/configuration/runnerConnectorDefinitions.xml");
	    m_runnerContext.registerShutdownHook();

	}
	return m_runnerContext;
    }

    /**
     * Closes initialized bean contexes
     */
    public static void close() {
	if (m_generalContext != null) {
	    m_generalContext.stop();
	    m_generalContext.close();
	    m_generalContext.destroy();
	    m_generalContext = null;
	}
	if (m_loggingContext != null) {
	    m_loggingContext.stop();
	    m_loggingContext.close();
	    m_loggingContext.destroy();
	    m_loggingContext = null;
	}
	if (m_loggingContextForClient != null) {
	    m_loggingContextForClient.stop();
	    m_loggingContextForClient.close();
	    m_loggingContextForClient.destroy();
	    m_loggingContextForClient = null;
	}
	if (m_runnerContext != null) {
	    m_runnerContext.stop();
	    m_runnerContext.close();
	    m_runnerContext.destroy();
	    m_runnerContext = null;
	}

    }

}